<?php

namespace app\admin\model;

use think\Model;


class Teacher extends Model
{





    // 表名
    protected $name = 'teacher';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'init';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'college',
        'type_text'
    ];

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }



    public function classes()
    {
        return $this->belongsTo('Department','department_id');
    }

    public function getCollegeAttr($v,$d){
        if(isset($d['department_id'])){
            return Department::get($d['department_id']) -> college_name;
        }else{
            return '-';
        }
    }

    public function department()
    {
        return $this->belongsTo('Department','department_id');
    }

    public function getTypeTextAttr($value, $data)
    {
        $type = [1=>'在编人员',2=>'编外人员',3=>'务工人员'];
        if(isset($data['type']))
            return $type[$data['type']];
        else
            return '-';
    }

    public static function get_info($username, $sid)
    {
        $sms = \app\admin\model\Teacher::
        where(['nickname' => $username, 'tel' => $sid])
            ->find();
        return $sms ? $sms : null;
    }

}
